const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: app.globalData.statusBarHeight+'px',
    customBar:app.globalData.customBar+'px',
    navheight:app.globalData.customBar-app.globalData.statusBarHeight+'px',
    key:'',
    lishi:[],
    listshow:false,
    listnone:false,
    lishistatus:true,
    intCurrPage:1,
    intPageCount:0,
    intRecordCount:0,
    srList:[],
    ismore:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (app.checkFjLogin) {
      this.getFeijiu_Get_QuesSS();
    }    
  },
  searchinput:function (e) {
    this.setData({
      key: e.detail.value
    })    
  },
  //把前4个关键字放到缓存里
  searchBtn:function (e) {
    var that = this;
    var FjLoginLog = wx.getStorageSync('FjLoginLog');
    if (!FjLoginLog.FjToken) {
      wx.navigateTo({ url: '../../pages/login/index' });return false;
    }
    var api_input = {
      lytype:3,
      KeyWord:this.data.key,
      UID:FjLoginLog.UID
    }
    app.getDaoPostN('api/FeijiuApp/Feijiu_Add_QuesSS',api_input,function (r) {
      
    })
    this.getFeijiu_Get_WDSSList();
    this.getFeijiu_Get_QuesSS();
    this.setData({
      lishistatus:false,
      srList:[]
    })
    //Feijiu_Add_QuesSS
    // var wd_search = wx.getStorageSync('wd_search');
    // if (wd_search.length > 0) {
    //     var index = wd_search.indexOf(this.data.key);
    //     if (index != -1) {
    //       wd_search.splice(index, 1);  //如果存在删除          
    //     }
    //     wd_search.unshift(this.data.key);//放到最前边
    //     if (wd_search.length > 4) {
    //       wd_search = wd_search.slice(0,4);//超过4个截取前4
    //     }
    //     wx.setStorageSync('wd_search',wd_search);
    // }else{
    //   wd_search = [];
    //   wd_search.push(this.data.key);//第一次直接装入数组
    //   wx.setStorageSync('wd_search',wd_search);
    // }
    // this.setData({
    //   lishi:wd_search
    // })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },
  getFeijiu_Get_WDSSList:function () {
    var that = this;
    var api_input = {
      intCurrPage:this.data.intCurrPage,
      KeyWord:this.data.key
    }
    app.getDaoPostN('api/FeijiuApp/Feijiu_Get_WDSSList',api_input,function (r) {
      if (r.code == 200) { 
      
        var data = r.data.data.ds;
        for (var i = 0; i < data.length; i++) {
          let res = new RegExp("("+that.data.key+")",'g');
          data[i].Title = data[i].Title.replace(res,"<span style='color:red;'>" + that.data.key+"</span>");          
        }
        var words = that.data.srList.concat(data);
        that.setData({          
          intPageCount:r.data.intPageCount,
          intRecordCount:r.data.intRecordCount,
          srList:words
        }) 
        if (r.data.intRecordCount == 0) {
          that.setData({
            listnone:true
          })
        }else{
          that.setData({
            listshow:true
          })          
        }       
      }
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      lishi:wx.getStorageSync('wd_search')
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  getFeijiu_Get_QuesSS:function () {
    var that = this;
    var FjLoginLog = wx.getStorageSync('FjLoginLog');
    if (!FjLoginLog.FjToken) {
      wx.navigateTo({ url: '../../pages/login/index' });return false;
    }
    var api_input = {
      lytype:3,
      KeyWord:this.data.key,
      UID:FjLoginLog.UID
    }
    app.getDaoPostN('api/FeijiuApp/Feijiu_Get_QuesSS',api_input,function (r) {
      
      if (r.code == 200) {
        that.setData({
          lishi:r.data
        })
      }
    })    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if ((this.data.intCurrPage+1) <= this.data.intPageCount) {
      this.setData({
        intCurrPage:this.data.intCurrPage+1
      })
      this.getFeijiu_Get_WDSSList();
    }else{
      this.setData({
        ismore:true
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  tosearch:function (e) {
    this.setData({
      key:e.currentTarget.dataset.key
    })
    this.searchBtn();
  },
  todetaile:function (e) {
    wx.navigateTo({
      url: 'detaile?tqid='+e.currentTarget.dataset.tqid
    })
  },
  back:function () {
    if (this.data.srList.length > 0 && this.data.lishi.length >0) {
      this.setData({
        srList:[],
        lishistatus:true
      })
    }else{
      wx.navigateBack()
    }    
  },
  toindex:function () {
    wx.redirectTo({
      url: 'index'
    })
  },
  // 删除单个关键字
  deleteKey:function (e) {
    var FjLoginLog = wx.getStorageSync('FjLoginLog');
    if (!FjLoginLog.FjToken) {
      wx.navigateTo({ url: '../../pages/login/index' });return false;
    }
    var that = this;
    var api_input = {
      deltype:1,
      ID:e.currentTarget.dataset.id,
      UID:FjLoginLog.UID
    }
    app.getDaoPostN('api/FeijiuApp/Feijiu_Del_QuesSS',api_input,function (r) {
      
      if (r.code == 200) {
        app.tishi('删除成功!');
        var lishi = that.data.lishi;
        lishi.splice(e.currentTarget.dataset.idx,1);
        that.setData({
          lishi:lishi
        })
      }else{
        app.tishi(r.message);
      }
    })     
  },
  //删除全部
  qingkong:function () {
    var FjLoginLog = wx.getStorageSync('FjLoginLog');
    if (!FjLoginLog.FjToken) {
      wx.navigateTo({ url: '../../pages/login/index' });return false;
    }
    var that = this;
    var api_input = {
      deltype:2,
      UID:FjLoginLog.UID
    }
    app.getDaoPostN('api/FeijiuApp/Feijiu_Del_QuesSS',api_input,function (r) {      
      if (r.code == 200) {
        app.tishi('删除成功!');
        that.setData({
          lishi:[]
        })
      }else{
        app.tishi(r.message);
      }
    }) 
  }
})